package com.sth.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sth.edu.client.VodClient;
import com.sth.edu.entity.Video;
import com.sth.edu.mapper.VideoMapper;
import com.sth.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-05
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    //根据课程id删除小节 TODO 删除视频
    @Override
    public void removeVideoByCourseId(String courseId) {
        // 1. 根据课程id 查出所有视频id
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.select("video_source_id");
        List<Video> videoList = baseMapper.selectList(wrapperVideo);
        if (!CollectionUtils.isEmpty(videoList)) {
            List<String> videoSourceIdList = videoList.stream().filter(video -> video.getVideoSourceId() != null).map(video -> video.getVideoSourceId()).collect(Collectors.toList());
            vodClient.deleteBatch(videoSourceIdList);
        }
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
